package com.bingyuan.aipassproject.view.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bingyuan.aipassproject.R;
import com.bingyuan.aipassproject.entity.SendDetailResponse;
import com.bingyuan.aipassproject.view.adapter.GetRobsAdapter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;

import java.util.ArrayList;

public abstract class SendDetailPop {

    private PopupWindow popupWindow;
    private Activity context;
    private Gson mGson;
    private SendDetailResponse response;

    /**
     * 构造函数
     *
     * @param context
     */
    public SendDetailPop(Activity context, String json) {
        this.context = context;
        mGson = new Gson();
        response = mGson.fromJson(json, SendDetailResponse.class);
    }

    /**
     * 显示警示框
     */
    @SuppressLint("SetTextI18n")
    public void show() {
        View popView = View.inflate(context, R.layout.pop_hit_gold_deatil, null);
        popupWindow = new PopupWindow(context);
        popupWindow.setAnimationStyle(R.style.popWindow_animation);//设置动画
        popupWindow.setOutsideTouchable(false);
        popupWindow.setBackgroundDrawable(null);
        int width = context.getWindowManager().getDefaultDisplay().getWidth();
        popupWindow.setWidth(width);
        popupWindow.setFocusable(true);
        popupWindow.setContentView(popView);
        popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);


        popView.findViewById(R.id.ivClose).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        TextView tvNum = popView.findViewById(R.id.tvNum);
        tvNum.setText(response.getStake_score());

        ImageView ivSendOver = popView.findViewById(R.id.ivSendOver);
        if (response.getStatus().equals("finish ")) {
            ivSendOver.setVisibility(View.VISIBLE);
        }
        //头像
        ImageView ivIcon = popView.findViewById(R.id.ivIcon);
        if (response.getSend_user().getFace().equals("false")) {
            Glide.with(context).load(R.drawable.icon_csc).into(ivIcon);
        } else {
            Glide.with(context).load(response.getSend_user().getFace())
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(ivIcon);
        }

        //用户名
        TextView tvUserName = popView.findViewById(R.id.tvUserName);
        tvUserName.setText(response.getSend_user().getNickname());

        //所选雷号
        TextView tvSelectNum = popView.findViewById(R.id.tvSelectNum);
        if (response.getSelect_num().size() == 1) {
            tvSelectNum.setText("雷：" + response.getSelect_num().get(0));
        } else {
            tvSelectNum.setText("雷：" + response.getSelect_num().get(0) + "," + response.getSelect_num().get(1));
        }

        //剩余数量
        TextView tvOverNum = popView.findViewById(R.id.tvOverNum);
        if (response.getSurplus() == 0) {
            tvOverNum.setText(response.getSplit_num() - response.getSurplus() + "/" + response.getSplit_num() + "，已领完");
        } else {
            tvOverNum.setText(response.getSplit_num() - response.getSurplus() + "/" + response.getSplit_num());
        }

        //中雷数
        TextView tvBoomNum = popView.findViewById(R.id.tvBoomNum);
        tvBoomNum.setText("中" + response.getProfit_num() + "雷");

        //列表
        RecyclerView recyclerView = popView.findViewById(R.id.robRecyclerView);
        ArrayList<SendDetailResponse.RobsBean> list = (ArrayList<SendDetailResponse.RobsBean>) response.getRobs();
        GetRobsAdapter adapter = new GetRobsAdapter();
        adapter.setNewInstance(list);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));

        popupWindow.setOnDismissListener(() -> onDisMiss());

    }

    /**
     * 消失
     */
    public abstract void onDisMiss();

    public void dissMiss() {
        popupWindow.dismiss();
    }

}
